package make.free.app.mcaro.Dialog;

import make.free.app.mcaro.R;
import make.free.app.mcaro.activity.Game;
import make.free.app.mcaro.bluetooth.BluetoothChatService;
import make.free.app.mcaro.bluetooth.DeviceListActivity;
import make.free.app.mcaro.game.EventTitle;
import android.app.Dialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.Toast;

public class Dialog_Choice_Player extends Dialog implements
		android.view.View.OnClickListener {

	private Context mContext;
	private Button mConect;
	private Button mStart;
	public static ImageView mChat;
	private RadioGroup radioSexGroup;
	private RadioGroup radioTurn;
	private Game activity;
	private BluetoothChatService mChatService;
	private ProgressDialog progressDialog;
	public static int chessmanID, turnID;

	public Dialog_Choice_Player(Context context,
			BluetoothChatService mChatService) {
		super(context);
		// TODO Auto-generated constructor stub
		mContext = context;
		activity = (Game) mContext;
		this.mChatService = mChatService;
		iniDialog();
	}

	/**
	 * init view dialog
	 */
	public void iniDialog() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);

		setCancelable(false);
		setContentView(R.layout.conect);
		progressDialog = new ProgressDialog(mContext);
		progressDialog.setMessage("Please watting player accept");
		radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
		radioTurn = (RadioGroup) findViewById(R.id.radioluot);
		mStart = (Button) findViewById(R.id.connect_btn_start);
		mStart.setOnClickListener(this);
		mChat = (ImageView) findViewById(R.id.connect_imv_Chat);
		mChat.setBackgroundResource(R.drawable.chat_offline);
		mChat.setOnClickListener(this);
		mConect = (Button) findViewById(R.id.connect_btn_search);
		mConect.setOnClickListener(this);

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		Intent serverIntent = null;
		switch (v.getId()) {

		case R.id.connect_btn_search:
			serverIntent = new Intent(mContext, DeviceListActivity.class);
			activity.startActivityForResult(serverIntent,
					Game.REQUEST_CONNECT_DEVICE_SECURE);
			// dismiss();
			break;
		case R.id.connect_imv_Chat:
			if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
				Toast.makeText(mContext, R.string.not_connected,
						Toast.LENGTH_SHORT).show();
			} else {
				Dialog_Chat dialog = new Dialog_Chat(mContext, mChatService);
				dialog.show();
			}
			break;
		case R.id.connect_btn_start:

			if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
				Toast.makeText(mContext, R.string.not_connected,
						Toast.LENGTH_SHORT).show();
			} else {
				Game.showProgressDialog();
				
				int selectedId = radioSexGroup.getCheckedRadioButtonId();
				if (selectedId == R.id.radiox)
					chessmanID = 0;
				else
					chessmanID = 1;

				int selectedTurnId = radioTurn.getCheckedRadioButtonId();
				if (selectedTurnId == R.id.radiolx)
					turnID = 0;
				else
					turnID = 1;
				

				String message = "1" + "-" + String.valueOf(chessmanID) + "-"
						+ String.valueOf(turnID);
				Log.e("Massage", message);
				sendMessage(message);
			}
			break;
		default:
			break;
		}
	}

	private void sendMessage(String message) {
		// Check that we're actually connected before trying anything
		if (mChatService.getState() != BluetoothChatService.STATE_CONNECTED) {
			Toast.makeText(mContext, R.string.not_connected, Toast.LENGTH_SHORT)
					.show();
			return;
		}
		if (message.length() > 0) {
			// Get the message bytes and tell the BluetoothChatService to write
			byte[] send = message.getBytes();
			mChatService.write(send);

		}
	}
 	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		mChatService.stop();
		EventTitle.Win = false;
		activity.finish();
	}
}
